const valimobilePhone = (rule, value, callback) => {
  if (value === "" || value == undefined) {
    callback(); // 手机号码
  } else {
    if (!/^1[3456789]\d{9}$/.test(value)) {
      callback(new Error($i18n.t("mesSalesOrder.rule9"))); // 手机号码
    } else {
      callback();
    }
  }
};
const validatePhone = (rule, value, callback) => {
  if (value === "" || value == undefined) {
    callback();
  } else {
    if (!/0\d{2,3}-\d{7,8}/.test(value)) {
      callback(new Error($i18n.t("mesSalesOrder.rule10")));
    } else {
      callback();
    }
  }
};

import { exportUrl, getDeliveryMethodList } from "@/api/order/mesSalesOrder";
import { uniqueKey, checkStr } from "@/utils/judge";
//  $i18n.t('common.add')/删除表单配置
export const formDescDialog = {
  data() {
    const _this = this;
    var validatePass = (rule, value, callback) => {
      setTimeout(() => {
        if (!checkStr(value, "english-number-")) {
          callback(new Error($i18n.t("mesSalesOrder.rule11")));
        } else {
          callback();
        }
      }, 100);
    };
    return {
      formDescDialog: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        rules: {
          salesOrderCode: [
            { validator: validatePass, trigger: ["change", "blur"] },
          ],
        },
        formDesc: {
          salesOrderCode: {
            type: "input",
            label: $i18n.t("mesSalesOrder.salesOrderCode"),
            layout: 12,
            disabled: (data) => {
              return !!data.codeStrategy || !!data.id;
            },
          },
          // customerCode: {
          //   type: "input",
          //   label: "客户编码",
          //   layout: 12,
          //   required: true,
          // },
          customerName: {
            type: "input",
            label: $i18n.t("mesSalesOrder.customerName"),
            layout: 12,
            disabled: true,
            // required: true,
          },
          telephone: {
            type: "input",
            label: $i18n.t("mesSalesOrder.telephone"),
            layout: 12,
            rules: [
              { validator: valimobilePhone, trigger: ["change", "blur"] },
            ],

            // required: true,
          },
          deliveryMethod: {
            type: "select",
            label: $i18n.t("mesSalesOrder.deliveryMethod"),
            layout: 12,
            required: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getDeliveryMethodList();
              return list.data.result;
            },
          },
          deliveryAddress: {
            type: "input",
            label: $i18n.t("mesSalesOrder.deliveryAddress"),
            layout: 12,
            required: true,
          },
          customerOrderCode: {
            type: "input",
            label: $i18n.t("mesSalesOrder.customerOrderCode"),
            layout: 12,
            required: false,
          },
          planDeliveryDate: {
            type: "date",
            label: $i18n.t("mesSalesOrder.planDeliveryDate"),
            layout: 12,
            required: true,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },
          // factoryCode: {
          //   type: "input",
          //   label: "工厂编码",
          //   layout: 12,
          //   required: false,
          // },
          remark: {
            type: "textarea",
            label: $i18n.t("common.remark"),
            layout: 24,
            required: false,
          },

          mesSalesOrderDetailList: {
            label: $i18n.t("mesSalesOrder.salesOrderDetails"),
            layout: 24,
            required: true,
            type: "table-editor",
            attrs: {
              // 上方,按钮,render
              topBtnRender: (h, solt) => {
                return (
                  <el-button
                    type="primary"
                    onClick={() => {
                      _this.addList();
                    }}
                  >
                    {$i18n.t("common.add")}
                  </el-button>
                );
              },
              // 校检规则
              rules: {
                lineNum: {
                  required: true,
                  message: $i18n.t("mesSalesOrder.inputLineNum"),
                },
                count: {
                  required: true,
                  message: $i18n.t("mesSalesOrder.inputCount"),
                },
              },
              isShowAdd: false,
              // 表格列
              //   字段已对完
              columns: [
                {
                  // el-table-column 的属性
                  type: "index",
                  width: 50,
                },
                {
                  // el-table-column 的属性
                  prop: "prodCode",
                  label: $i18n.t("mesSalesOrder.prodCode"),
                },
                {
                  prop: "prodName",
                  label: $i18n.t("mesSalesOrder.prodName"),
                  showOverflowTooltip: true,
                },
                {
                  prop: "lineNum",
                  label: $i18n.t("mesSalesOrder.lineNum"),
                  // 当有 content 属性时, 可以指定相应的组件
                  content: {
                    // type 可以为全局注册的组件名或者组件引用
                    type: "el-input-number",
                    // attrs 是组件的属性
                    attrs: {
                      placeholder: $i18n.t("mesSalesOrder.inputLineNum"),
                      controlsPosition: "right",
                      min: 1,
                    },
                  },
                },
                {
                  prop: "count",
                  label: $i18n.t("mesSalesOrder.count"),
                  // 当有 content 属性时, 可以指定相应的组件
                  content: {
                    // type 可以为全局注册的组件名或者组件引用
                    type: "el-input-number",
                    // attrs 是组件的属性
                    attrs: {
                      placeholder: $i18n.t("mesSalesOrder.inputCount"),
                      min: 1,
                      controlsPosition: "right",
                    },
                  },
                },
              ],

              // 新行默认值
              newColumnValue: {
                lineNum: 0,
                count: 0,
              },
            },
          },
        },
      },
    };
  },
  methods: {
    /**
     * @description   $i18n.t('common.add')表格数据
     * @author bing
     * @date 2022-03-18 08:53
     */
    addList() {
      this.$refs.dialogTable.baseGoodsDialogVisible2 = true;
    },
    /**
     * @description  选中表格 返回的数据
     * @author bing
     * @date 2022-03-18 10:20
     * @param {Array} data
     */
    responseList(data) {
      let list = [
        ...(this.formDataDialog?.mesSalesOrderDetailList ?? []),
        ...data.map((ite) => {
          return {
            ...ite,
            prodCode: ite.goodsCode,
            prodName: ite.goodsName,
          };
        }),
      ];

      // 筛选重复值,插入
      this.$set(
        this.formDataDialog,
        "mesSalesOrderDetailList",
        uniqueKey(list, "prodCode")
      );
    },
  },
};
